#include <stdio.h>
#include <stdlib.h>
#include "cfort.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>


#include "windows.h"
typedef void (*P_ANSYS_MKL_FINISH_FUNC)(int IER);
typedef void (*P_ANSYS_MKL_INFORMATION_FUNC)(int IER);
typedef void (*P_ANSYS_MKL_INITIZATION_FUNC)(int ITYP,char *p,int NN, int NOUT, int IEXTR, int IER);
typedef void (*P_ANSYS_MKL_SYSINI_FUNC)(int I, int IER);
typedef void (*P_ANSYS_MKL_SYSELE_FUNC)(int NDDL, int *LM, int NDDL1, int *LM1, int IER);
typedef void (*P_ANSYS_MKL_SYSFINISH_FUNC)(int I, int IER);
typedef void (*P_ANSYS_MKL_NUMINITIZATION_FUNC)(int IER);
typedef void (*P_ANSYS_MKL_NUMARRYZ_FUNC)(int IER);
typedef void (*P_ANSYS_MKL_NUMELE_FUNC)(int NDDL, int *LM, int NDDL1, int *LM1, double *STH, int IER);
typedef void (*P_ANSYS_MKL_NUMFACTORY_FUNC)(double DEPS, int IER);
typedef void (*P_ANSYS_MKL_LINEARSOLVER_FUNC)(double *R, double *P, int NIQ, double DPREC, int NITER, int IER);

static HMODULE m_pDLLhInstance = NULL;

int LoadDLL()
{
	if(!m_pDLLhInstance)
	{
		HMODULE hModule = LoadLibrary("ASTER_ANSYS.dll");
		if (!hModule)
		{
			return 0;
		} 
		else
		{
			m_pDLLhInstance = hModule;
		}
	}
	return 1;
}

void ANSYS_MKL_FINISH(int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_FINISH_FUNC pFunc = (P_ANSYS_MKL_FINISH_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_FINISH");
		if (pFunc != NULL)
		{
			pFunc(IER);
		}
	}
}
void ANSYS_MKL_INFORMATION(int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_INFORMATION_FUNC pFunc = (P_ANSYS_MKL_FINISH_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_INFORMATION");
		if (pFunc != NULL)
		{
			pFunc(IER);
		}
	}
}
void ANSYS_MKL_INITIZATION(int ITYP,char *p,int NN, int NOUT, int IEXTR, int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_INITIZATION_FUNC pFunc = (P_ANSYS_MKL_INITIZATION_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_INITIZATION");
		if (pFunc != NULL)
		{
			pFunc(ITYP, p, NN, NOUT, IEXTR, IER);
		}
	}
}

void ANSYS_MKL_SYSINI(int I, int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_SYSINI_FUNC pFunc = (P_ANSYS_MKL_SYSINI_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_SYSINI");
		if (pFunc != NULL)
		{
			pFunc(I, IER);
		}
	}
}

void ANSYS_MKL_SYSELE(int NDDL, int *LM, int NDDL1, int *LM1, int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_SYSELE_FUNC pFunc = (P_ANSYS_MKL_SYSELE_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_SYSELE");
		if (pFunc != NULL)
		{
			pFunc(NDDL, LM, NDDL1, LM1, IER);
		}
	}
}

void ANSYS_MKL_SYSFINISH(int I, int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_SYSFINISH_FUNC pFunc = (P_ANSYS_MKL_SYSFINISH_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_SYSFINISH");
		if (pFunc != NULL)
		{
			pFunc(I, IER);
		}
	}
}

void ANSYS_MKL_NUMINITIZATION(int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_NUMINITIZATION_FUNC pFunc = (P_ANSYS_MKL_NUMINITIZATION_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_NUMINITIZATION");
		if (pFunc != NULL)
		{
			pFunc(IER);
		}
	}
}

void ANSYS_MKL_NUMARRYZ(int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_NUMARRYZ_FUNC pFunc = (P_ANSYS_MKL_NUMARRYZ_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_NUMARRYZ");
		if (pFunc != NULL)
		{
			pFunc(IER);
		}
	}
}

void ANSYS_MKL_NUMELE(int NDDL, int *LM, int NDDL1, int *LM1, double *STH, int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_NUMELE_FUNC pFunc = (P_ANSYS_MKL_NUMELE_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_NUMELE");
		if (pFunc != NULL)
		{
			pFunc(NDDL, LM, NDDL1, LM1, STH, IER);
		}
	}
}

void ANSYS_MKL_NUMFACTORY(double DEPS, int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_NUMFACTORY_FUNC pFunc = (P_ANSYS_MKL_NUMFACTORY_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_NUMFACTORY");
		if (pFunc != NULL)
		{
			pFunc(DEPS, IER);
		}
	}
}

void ANSYS_MKL_LINEARSOLVER(double *R, double *P, int NIQ, double DPREC, int NITER, int IER)
{
	if(LoadDLL())
	{
		P_ANSYS_MKL_LINEARSOLVER_FUNC pFunc = (P_ANSYS_MKL_LINEARSOLVER_FUNC)GetProcAddress(m_pDLLhInstance, "ANSYS_MKL_LINEARSOLVER");
		if (pFunc != NULL)
		{
			pFunc(R, P, NIQ, DPREC, NITER,IER);
		}
	}
}


